package org.example.domain.strategy.service.raffle;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.domain.strategy.model.entity.RaffleFactorEntity;
import org.example.domain.strategy.model.entity.RuleActionEntity;
import org.example.domain.strategy.model.entity.RuleMatterEntity;
import org.example.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import org.example.domain.strategy.model.vo.RuleTreeVO;
import org.example.domain.strategy.model.vo.StrategyAwardRuleModelVO;
import org.example.domain.strategy.model.vo.StrategyAwardStockKeyVO;
import org.example.domain.strategy.repository.IStrategyRepository;
import org.example.domain.strategy.service.AbstractRaffleStrategy;
import org.example.domain.strategy.service.armory.IStrategyDispatch;

import org.example.domain.strategy.service.rule.chain.ILogicChain;
import org.example.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.example.domain.strategy.service.rule.chain.impl.DefaultLogicChain;

import org.example.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.example.domain.strategy.service.rule.tree.factory.engine.impl.DecisionTreeEngine;
import org.springframework.stereotype.Service;


/**
 * 具体抽奖前置规则过滤实现
 */
@Service
@Slf4j
public class DefaultRaffleStrategy extends AbstractRaffleStrategy {

    public DefaultRaffleStrategy(IStrategyRepository repository, IStrategyDispatch strategyDispatch, DefaultChainFactory defaultChainFactory,DefaultTreeFactory defaultTreeFactory) {
        super(repository, strategyDispatch, defaultChainFactory,defaultTreeFactory);
    }

    /**
     * 抽奖责任链具体实现
     *
     * @param userId
     * @param StrategyId
     * @return
     */
    @Override
    public DefaultChainFactory.strategyAwardVO raffleChain(String userId, Long StrategyId) {

        ILogicChain logicChain = defaultChainFactory.openLogicChain(StrategyId);
        return logicChain.logic(userId, StrategyId);
    }


    @Override
    public DefaultTreeFactory.StrategyAwardData raffleTree(String userId, Long strategyId, Integer awardId) {

        StrategyAwardRuleModelVO strategyAwardRuleModelVO = repository.queryRuleModels(strategyId, awardId);
        if (strategyAwardRuleModelVO == null) return DefaultTreeFactory.StrategyAwardData.builder()
                .awardId(awardId)
                .build();

        RuleTreeVO ruleTreeVO = repository.queryRuleTreeByTreeId(strategyAwardRuleModelVO.getRuleModels());
        if(ruleTreeVO == null) {
            throw new RuntimeException("配置错误");
        }
        DecisionTreeEngine decisionTreeEngine = defaultTreeFactory.startEngine(ruleTreeVO);
        return decisionTreeEngine.process(userId, strategyId, awardId);



    }

    @Override
    public StrategyAwardStockKeyVO takeQueue() throws InterruptedException {
        return repository.takeQueue();
    }

    @Override
    public void updateStrategyAwardStockKey(Long strategyId, Integer awardId) {
        repository.updateStrategyAwardStockKey(strategyId,awardId);
    }
}
